package com.sound.ampache;

/* Copyright (c) 2010 Brian Kirkman <kirkman.brian@gmail.com>
*
* +------------------------------------------------------------------------+
* | This program is free software; you can redistribute it and/or          |
* | modify it under the terms of the GNU General Public License            |
* | as published by the Free Software Foundation; either version 3         |
* | of the License, or (at your option) any later version.                 |
* |                                                                        |
* | This program is distributed in the hope that it will be useful,        |
* | but WITHOUT ANY WARRANTY; without even the implied warranty of         |
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          |
* | GNU General Public License for more details.                           |
* |                                                                        |
* | You should have received a copy of the GNU General Public License      |
* | along with this program; if not, write to the Free Software            |
* | Foundation, Inc., 59 Temple Place - Suite 330,                         |
* | Boston, MA  02111-1307, USA.                                           |
* +------------------------------------------------------------------------+
*
* Special thanks to the following: 
* Kevin Purdy, author of Amdroid, for much of the code used in this project
* 
* Jorge Ramon, author of MiamiCoder blog, for many great BlackBerry examples
* published on his blog.
* 
* 
*/

import java.util.Vector;

import com.sound.ampache.objects.Playlist;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.ObjectListField;


class PlaylistsScreen extends ItemsScreen implements HTTPTransportListener {
    
	private Playlist[] playlists;
	private PlaylistsListField playlistsList;
	private SongsScreen songsScreen;
	public String screenTitle = "Playlists";
	
	Font mainFont = Font.getDefault().derive(Font.PLAIN, 24);
	
    private MenuItem viewSongsMenu = new MenuItem("View Songs",100,10) {
        public void run() {
            viewSongs();
        } 
     };
     
     private MenuItem addSongsMenu = new MenuItem("Add Songs to Playlist",100,10) {
         public void run() {
             addSongs();
         } 
      };
    
	PlaylistsScreen()
	{
		playlistsList = new PlaylistsListField();
		ItemsScreen.itemsList = playlistsList;
        setTitle( new LabelField( screenTitle , LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH ) );
        playlistsList.setEmptyString("No Playlists", DrawStyle.HCENTER);
        this.add(itemsList);         
	}
	
	
	public void getPlaylists() {
		getPlaylists("playlists", "");
	}
	
	
	public void getPlaylists(String filter) {
		getPlaylists("playlists", filter);
	}
	
	
	public void getPlaylists(String method, String filter){
		setTitle(screenTitle);
		playlistsList.set(null); 
		statusDlg.show();
	    ampacheberry.comm.setHTTPTransportListener(this);
	    ampacheberry.comm.send(method, filter); 
	}
	
	
    protected void makeMenu(Menu menu, int instance) {
        menu.add(viewSongsMenu);
        menu.add(addSongsMenu);
        menu.add(MenuItem.separator(addSongsMenu.getOrdinal() + 1));
        super.makeMenu(menu,instance);
    }
	
	
    private class PlaylistsListField extends ObjectListField {
        
        private Bitmap icon = Bitmap.getBitmapResource("img/playlist.png");  
        
        // We are going to take care of drawing the item.
        public void drawListRow(ListField listField, Graphics graphics, 
                int index, int y, int width) {     

            int offsetY = (this.getRowHeight() - icon.getHeight())/2;
            graphics.drawBitmap(1,y + offsetY, icon.getWidth(),
                    icon.getHeight(),icon,0,0);
            
            graphics.drawText("Playlist: " + playlists[index].name, 
            		icon.getWidth() + 4, y + 1, DrawStyle.ELLIPSIS,
            		width - icon.getWidth() + 2);
            graphics.drawText("Owner: " + playlists[index].owner, 
                    icon.getWidth() + 4, y + fontSize + 2,
                    DrawStyle.ELLIPSIS, width - icon.getWidth() + 2);
            graphics.drawText("Tracks: " + playlists[index].count, 
                    icon.getWidth() + 4, y + (2 * fontSize) + 2,
                    DrawStyle.ELLIPSIS, width - icon.getWidth() + 2);
            graphics.drawLine(0, y + rowHeight - 1, listField.getWidth(),
            		y + rowHeight - 1);
        }   
    };
	
    
	public void processResponse(final Vector data) {
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run()
            {
                statusDlg.close();
                playlists = new Playlist[data.size()];
    			data.copyInto(playlists);
    			playlistsList.set(playlists);
            }
        });
	}
	
	
    private void viewSongs() {
    	int selected = playlistsList.getSelectedIndex();
    	
    	if (null == songsScreen) {
    		songsScreen = new SongsScreen();
    	}
    	songsScreen.setRowHeight();
    	songsScreen.screenTitle = "Songs on " + playlists[selected].name + " playlist";
    	UiApplication.getUiApplication().pushScreen(songsScreen);
        songsScreen.getSongs("playlist_songs", playlists[selected].id);
    }
    
    
    private void addSongs() {
    	int selected = playlistsList.getSelectedIndex();
    	ampacheberry.playlistSong.addPlaylistToPlaylist(playlists[selected]);
    }
} 
